package com.zntg.schedule;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zntg.bo.FundFeneSureBook;
import com.zntg.bo.SpciProdType3Bo;
import com.zntg.bo.TSpciProdBo;
import com.zntg.contants.FundFeneSureBookContants;
import com.zntg.entity.TSpciProdEntity;
import com.zntg.entity.TSpciProdNewEntity;
import com.zntg.entity.TblobstorageNewEntity;
import com.zntg.mapper.crmdb.TblobtSorageMapper;
import com.zntg.mapper.odsdb.HangZhouWangHeMapper;
import com.zntg.mapper.odsdb.TSpciProdMapper;
import com.zntg.pdf.CreatePdfUtilTemplate3;
import com.zntg.utils.ComponentFactory;
import com.zntg.utils.DateUtils;
import com.zntg.utils.string.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/** 
* @Description: 对应ods库T_SPCI_PROD这张表的SPCI_TYE = '3'的份额确认书生成功能
* @Author: zhengyunfei
* @Date: 2019/6/12  
* @time:13:34        
*/ 
@Component
public class Template3ScheduledTask {
    @Value("${server.port}")
    String port;
    /**电子签章生成路径*/
    @Value("${template.targetPath}")
    String targetPath;
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(Template3ScheduledTask.class);

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

   //@Scheduled(cron = "0 0 1 * * ?")   //每天凌晨1点执行
   @Scheduled(cron = "0 0/5 * * * ?")    //每10分钟执行一次
   /*@Scheduled(fixedRate = 50000000)*/
    public void task3() {
        try {
            System.out.println("Template3ScheduledTask计划任务开始执行，规则5分钟1次，当前执行时间==="+ DateUtils.getCurrentDate());
            TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
            HangZhouWangHeMapper hangZhouWangHeMapper = ComponentFactory.getBean(HangZhouWangHeMapper.class);
           TSpciProdMapper tSpciProdMapper = ComponentFactory.getBean(TSpciProdMapper.class);
            //获取要生成的数据
            List<SpciProdType3Bo> list = hangZhouWangHeMapper.findAllList();
            List<FundFeneSureBook> fundFeneSureBookList=null;
            Gson gson=new Gson();
            if(list.size()>0){
                System.out.println("ods库T_SPCI_PROD这张表的SPCI_TYE = '3'的份额确认书开始生成需要生成认购书的总数===================================="+list.size());
                fundFeneSureBookList=new ArrayList<>();
                for(int i=0;i<list.size();i++){
                    SpciProdType3Bo temleteBo=list.get(i);
                    String lMandatorId=temleteBo.getlMandatorId();//管理人ID
                    TSpciProdBo tSpciProdBo=new TSpciProdBo();
                    tSpciProdBo.setProdId(lMandatorId);
                    TSpciProdEntity tSpciProdEntity=tSpciProdMapper.findByBo(tSpciProdBo);
                    FundFeneSureBook fundFeneSureBook=SpciProdType3Bo.getFeneBook(temleteBo);
                    String cCustType=temleteBo.getcCustType();//0：企业 1：个人
                    if(!StringUtil.isNullOrEmpty(tSpciProdEntity)&&!StringUtil.isNullOrEmpty(tSpciProdEntity.getProdId())){
                        //过滤掉新的逻辑，新的是根据word模板生成的，但是查询语句都是一个，所以需要过滤掉
                        System.out.println("杭州望和认购书类型cCustType========================="+cCustType);
                        if("0".equals(cCustType)){
                            fundFeneSureBook.setSourcePath(tSpciProdEntity.getPurLettComp());
                        }else{
                            fundFeneSureBook.setSourcePath(tSpciProdEntity.getPurLett());
                        }
                        //根据产品编号查询生成的认购书或者追加书模板的名称，配置在T_SPCI_PROD表里面，此外需要将模板放在项目service_electronic_signatu的reresources下面
                        //查询crm附件序号
                        String cSubKeyId=temleteBo.getlBenserialNo();
                        fundFeneSureBook.setPkId(cSubKeyId);
                        String lStorageid=tblobtSorageMapper.getTblobStorageSeq()+"";
                        fundFeneSureBook.setlStorageid(lStorageid);
                        fundFeneSureBook.setCyType(FundFeneSureBookContants.SPCI_TYE_3);
                        fundFeneSureBookList.add(fundFeneSureBook);
                    }

                }
                if(!StringUtil.isNullOrEmpty(fundFeneSureBookList)&&fundFeneSureBookList.size()>0){
                    String jsonData= gson.toJson(fundFeneSureBookList);
                    String resultJson=createSignFeiJingZhiProd(jsonData);
                    //处理返回结果
                    List <FundFeneSureBook> resultBackList=gson.fromJson(resultJson,new TypeToken<List<FundFeneSureBook>>(){}.getType());
                    //定时任务可以做耗时操作，包括做生成数据库报表、文件IO等等需要定时执行的逻辑
                    List<TblobstorageNewEntity> tblobstorageEntityList=new ArrayList<>();
                    System.out.println("一共多少条数据======================"+resultBackList.size());
                    for(int i=0;i<resultBackList.size();i++){
                        FundFeneSureBook fundFeneSureBook=resultBackList.get(i);
                        String storId=fundFeneSureBook.getlStorageid();//上述sql的
                        //生成附件表到Crmku
                        TblobstorageNewEntity tblobstorageBo=new TblobstorageNewEntity();
                        tblobstorageBo.setlStorageid(storId+"");
                        String cSubkeyId=fundFeneSureBook.getPkId();
                        tblobstorageBo.setcSubkeyid(cSubkeyId);
                        String fileName=fundFeneSureBook.getFileName(); //生成的文件名
                        String fileSize=fundFeneSureBook.getFileSize();//文件大小
                        tblobstorageBo.setcFilename(fileName);
                        tblobstorageBo.setlFilesize(fileSize);
                        tblobstorageEntityList.add(tblobstorageBo);
                        tblobtSorageMapper.addFeiJingZhiLeiProd(tblobstorageBo);
                    }
                    System.out.println(list.size()+"条任务处理完成=======================Template3ScheduledTask");
                }


            }else{
               // System.out.println("我什么事都不用做，等待下次再说吧");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
    public String createSignFeiJingZhiProd(String  jsonData ){
        //生成电子签章
        String resultJson="";
        try {
            System.out.println("杭州望和产品生成认购书开始======================================参数="+jsonData);
            Gson gson=new Gson();
            List <FundFeneSureBook> list=gson.fromJson(jsonData,new TypeToken<List<FundFeneSureBook>>(){}.getType());
            List <FundFeneSureBook> result=new ArrayList<>();
            if(null!=list){
                for(int i=0;i<list.size();i++){
                    FundFeneSureBook fundFeneSureBook=list.get(i);
                    fundFeneSureBook.setTargetPath(targetPath);
                    FundFeneSureBook resultBo= CreatePdfUtilTemplate3.createPdf(fundFeneSureBook);
                    result.add(resultBo);
                }
                resultJson=gson.toJson(result);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultJson;
    }
}

